home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / acc.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  157 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    acc -
  19.  *        Software accumulation buffer implementation follows
  20.  *
  21.  *
  22.  *            Paul Haeberli - 1989
  23.  *
  24.  *    exports
  25.  *
  26.     void newacc(nx,ny)
  27.     void clearacc()
  28.     void addltoacc(l)
  29.     void addtoacc()
  30.     void avgacc(l)
  31.     void saveacc(name)
  32.     ACCPNTS *openpnts(name)
  33.     int numpnts(ap)
  34.     void getaccpnt(ap,n,xo,yo)
  35.  *
  36.  */
  37. #include "gl.h"
  38. #include "stdio.h"
  39. #include "acc.h"
  40.  
  41. static int nacc, xsize, ysize;
  42. static short *accr;
  43. static short *accg;
  44. static short *accb;
  45. static unsigned long *lbuf;
  46.  
  47. void clearacc()
  48. {
  49.     int n;
  50.  
  51.     nacc = 0;
  52.     n = xsize*ysize;
  53.     bzero(accr,n*sizeof(short));
  54.     bzero(accg,n*sizeof(short));
  55.     bzero(accb,n*sizeof(short));
  56. }
  57.  
  58. void newacc(nx,ny)
  59. int nx, ny;
  60. {
  61.     xsize = nx;
  62.     ysize = ny;
  63.     accr = (short *)mymalloc(xsize*ysize*sizeof(short));
  64.     accg = (short *)mymalloc(xsize*ysize*sizeof(short));
  65.     accb = (short *)mymalloc(xsize*ysize*sizeof(short));
  66.     lbuf = (unsigned long *)mymalloc(xsize*ysize*sizeof(long));
  67.     clearacc();
  68. }
  69.  
  70. void addltoacc(l)
  71. unsigned long *l;
  72. {
  73.     short *r, *g, *b;
  74.     int n;
  75.  
  76.     nacc++;
  77.     n = xsize*ysize;
  78.     r = accr;
  79.     g = accg;
  80.     b = accb;
  81.     while(n--) {
  82.     *r++ += ((*l)>>0) & 0xff;
  83.     *g++ += ((*l)>>8) & 0xff;
  84.     *b++ += ((*l)>>16) & 0xff;
  85.     l++;
  86.     }
  87. }
  88.  
  89. void addtoacc()
  90. {
  91.     lrectread(0,0,xsize-1,ysize-1,lbuf);
  92.     addltoacc(lbuf);
  93. }
  94.  
  95. void avgacc(l)
  96. unsigned long *l;
  97. {
  98.     int n;
  99.     short *r, *g, *b;
  100.     int ar, ag, ab;
  101.  
  102.     r = accr;
  103.     g = accg;
  104.     b = accb;
  105.     n = xsize*ysize;
  106.     while(n--) {
  107.     ar = *r++/nacc;
  108.     ag = *g++/nacc;
  109.     ab = *b++/nacc;
  110.     *l++ = (ar<<0)+(ag<<8)+(ab<<16);
  111.     }
  112. }
  113.  
  114. void saveacc(name)
  115. char *name;
  116. {
  117.     avgacc(lbuf);
  118.     longstoimage(lbuf,xsize,ysize,3,name);
  119. }
  120.  
  121. ACCPNTS *openpnts(name)
  122. char *name;
  123. {
  124.     int i;
  125.     FILE *inf;
  126.     ACCPNTS *ap;
  127.  
  128.     inf = fopen(name,"r");
  129.     if(!inf) {
  130.     fprintf(stderr,"openpnts: can't open input file %s\n",name);
  131.     exit(1);
  132.     }
  133.     ap = (ACCPNTS *)mymalloc(sizeof(ACCPNTS));
  134.     fscanf(inf,"%d\n",&ap->npoints);
  135.     ap->pdata = (float *)mymalloc(2*ap->npoints*sizeof(float));
  136.     for(i=0; i<ap->npoints; i++) 
  137.     fscanf(inf,"%f %f\n",ap->pdata+(2*i),ap->pdata+(2*i+1));
  138.     fclose(inf);
  139.     return ap;
  140. }
  141.  
  142. int numpnts(ap)
  143. ACCPNTS *ap;
  144. {
  145.     return ap->npoints;
  146. }
  147.  
  148. void getaccpnt(ap,n,xo,yo)
  149. ACCPNTS *ap;
  150. int n;
  151. float *xo, *yo;
  152. {
  153.     n = n%ap->npoints;
  154.     *xo = ap->pdata[2*n];
  155.     *yo = ap->pdata[2*n+1];
  156. }
  157.